package eu.reply.android.flashm;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

import eu.reply.android.flashm.exceptions.BEServiceNetException;

public class GCMIntentService extends GCMBaseIntentService {
	
	public GCMIntentService(){
		
	}

	@Override
	protected void onError(Context arg0, String arg1) {
		Log.e("GCMIntentService", arg1);

	}

	@Override
	protected void onMessage(Context arg0, Intent arg1) {
		// message received, get payload from extras
		
		Bundle bundle = arg1.getExtras();
		
		String name = bundle.getString("name");
		String description = bundle.getString("description");
		String location = bundle.getString("location");
		
		displayNotifcation(arg0,"Flash Mob Berlin","Go dancing to Alexanderplatz dressed in pink!!","Alexander platz Nr.1");
	}

	@Override
	protected void onRegistered(Context arg0, String arg1) {
		// TODO Temoc: check if the BE integration works
		Log.i("GCMIntentService", "RegistrationId" + arg1);
		
		try {
			BEAPI.registerUser("Andrea", arg1);
		} catch (BEServiceNetException e) {
			Log.e("GCMIntentService", "Error while sending registration ID: " +e.toString());
		}
		

	}

	@Override
	protected void onUnregistered(Context arg0, String arg1) {
		// TODO Unregister from server

	}

	public static void displayNotifcation(Context context,String eventName, String description,String location){
		
		//specify intent launch after clicking in the notification
		 Intent newIntent = new Intent(context,EventDetailsActivity.class);
		 int pIntentFlags= Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP;
		 PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, newIntent, pIntentFlags);
		 
		 //create a notification
		NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);;
		//Notification notification = new Notification(iconId, tickerText, System.currentTimeMillis());^
		
		Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.notif_placeholder);
		
		 Notification notification = new Notification.Builder(context)
         .setContentTitle(eventName)
         .setTicker("New Flash Mob!")
         .setContentText(description)
         .setSmallIcon(R.drawable.statusbar_icon_events )
         .setLargeIcon(largeIcon)
         .setContentIntent(pendingIntent)
         .build();
		
		 notification.flags |= Notification.FLAG_AUTO_CANCEL;
		 
		 
		 //notification.number =nrEntries;
		notificationManager.notify(getNextNotifID(context), notification);
		 
	}
	
	public static int getNextNotifID(Context context){
		int id=1;
		
		SharedPreferences sPref=context.getSharedPreferences("app_preferences", MODE_PRIVATE);
		id= sPref.getInt("notif_id", 1);
		
		int newID=id++;
		Editor editor= sPref.edit();
		editor.putInt("notif_id", newID);
		editor.apply();
		
		return id;
	}
}
